﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Entity;
using Web.Framework;
using System.Web.Security;
using System.Web.Configuration;
using Entity.Admin;
using Core.ZTree;
using Core;

namespace Service
{
    public class UserService : BaseService<User>
    {
        public UserService(DBEntities db)
            : base(db)
        {

        }

        public static bool ValidateUser(string p, string p_2)
        {
            return p == "admin" && p_2 == "admin";
            //throw new NotImplementedException();
        }

        public override User Create(User entity)
        {
            if (entity == null)
            {
                throw new ArgumentNullException("entity", "entity不能为null");
            }
            if (string.IsNullOrWhiteSpace(entity.ID))
            {
                entity.ID = Guid.NewGuid().ToString();
            }
            if (entity.CreateDate == DateTime.MinValue || entity.CreateDate == null)
            {
                entity.CreateDate = DateTime.Now;
            }
            entity.Password = FormsAuthentication.HashPasswordForStoringInConfigFile(entity.UserName + entity.Password, "MD5");
            //username不能重复
            if (db.Users.Any(p => p.UserName == entity.UserName))
            {
                throw new ModelCheckException("UserName", "用户名不能重复");
            }
            //db.Users.Add(entity);
            //db.SaveChanges();
            //return entity;
            return base.Create(entity);
        }

        public override User Save(User entity)
        {
            if (entity == null)
            {
                throw new ArgumentNullException("entity", "entity不能为null");
            }
            var user = db.Users.AsNoTracking().FirstOrDefault(p => p.ID == entity.ID);
            if (user != null)
            {
                if (user.Password != entity.Password)
                {
                    entity.Password = FormsAuthentication.HashPasswordForStoringInConfigFile(entity.UserName + entity.Password, "MD5");
                }
                if (entity.IslockedOut && user.IslockedOut == false)
                {
                    entity.LastLockoutDate = DateTime.Now;
                }
            }
            //username不能重复
            if (db.Users.Any(p => p.UserName == entity.UserName && p.ID != entity.ID))
            {
                throw new ModelCheckException("UserName", "用户名不能重复");
            }
            //db.Entry(entity).State = System.Data.EntityState.Modified;
            //db.SaveChanges();
            //return entity;
            return base.Save(entity);
        }

        public User CreateOrSave(User entity)
        {
            if (db.Users.Any(p => p.ID == entity.ID))
            {
                return Save(entity);
            }
            else
            {
                return Create(entity);
            }
        }

        public void ChangePassword(string userName, string oldPassword, string newPassword, string confirmPassword)
        {
            var user = db.Users.FirstOrDefault(p => p.UserName == userName);
            if (user != null)
            {
                if (user.Password == FormsAuthentication.HashPasswordForStoringInConfigFile(userName + oldPassword, "MD5"))
                {
                    user.Password = newPassword;
                    this.Save(user);
                }
                else
                {
                    throw new ModelCheckException("oldPassword", "原始密码不正确");
                }
            }
            else
            {
                throw new ModelCheckException("", "用户不存在");
            }
        }

        public static List<TreeNode> GetTreeNode(List<User> users, string rootName)
        {
            return new List<TreeNode>();
        }
    }
}
